import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import index from '../views/index.vue'

Vue.use(VueRouter)

// 嵌套路由

const routes = [
  {
    path: '/',
    name: 'index',
    component: index,
    redirect:'home',
    meta:{
      keep:true
    },
    children:[{
      path:'home',
      name:'home',
      component:HomeView
    },  {
      path: 'goods',
      name: 'goods',
      component: () => import('../views/goods.vue')
    },
    {
      path: 'my',
      name: 'my',
      component: () => import('../views/my.vue')
    }]
     
  },
  {
    path: '/city',
    name: 'city',
    component: () => import('../views/city.vue'),
    meta:{
      keep:true
    }
  },
  {
    path: '/shopList',
    name: 'shopList',
    component: () => import('../views/shopList.vue')
  },
  {
    path: '/filmsIntro',
    name: 'filmsIntro',
    component: () => import('../views/filmsIntro.vue')
  },
  {
    path: '/filmsOrder',
    name: 'filmsOrder',
    component: () => import('../views/filmsOrder.vue')
  },
  {
    path: '/cinema',
    name: 'cinema',
    component: () => import('../views/cinema.vue')
  },
  {
    path: '/chooseSeat',
    name: 'chooseSeat',
    component: () => import('../views/chooseSeat.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('../views/reg.vue')
  },
  {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import('../views/userInfo.vue'),
    meta:{
      auth:true
    }
  },
  {
    path: '/pay',
    name: 'pay',
    component: () => import('../views/pay.vue'),
    meta:{
      auth:true
    }
  },
  {
    path: '/paySuccess',
    name: 'paySuccess',
    component: () => import('../views/paySuccess.vue'),
    meta:{
      auth:true
    }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/order.vue'),
    meta:{
      auth:true
    }
  }

]

const router = new VueRouter({
  routes
});

//  路由切换前  

//  1. 切换时  判断一下是否是需要登录才能访问的页面  


router.beforeEach((to,from,next)=>{

// to  去哪里
// from  从哪来
// next 后续操作

//  如果你要去的页面需要验证 权限 
if(to.meta.auth){
  let token = localStorage.getItem('xl-token')
  let uid = localStorage.getItem('xl-uid')

    let hasAuth = token&&uid

  //   如果没权限 并且去的也不是登录页   
    if(!hasAuth&&to.name!='login'){
    console.log('去登录')
      // next({name:'login'})

      router.replace('/login')
        
    }else{

      next()

    }

}else{
   //  不是需要验证权限的页面 直接放行  
  next()
}


console.log(to)
console.log(from)



})



export default router
